Fix potential crash when extracting URI path in Keychain#187
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors four duplicated, crash-prone inline URI path extractions in Keychain.cs into a single GetUriPathBytes helper that safely handles URIs whose joined segments are shorter than 2 characters (e.g., host-only URIs).
Changes:
- Adds
GetUriPathByteshelper that returns an empty byte array when the URI has no path beyond/. - Replaces four
string.Join("", uri.Segments).Substring(1)occurrences with calls to the helper.
The pattern string.Join("", uri.Segments).Substring(1) would throw
an ArgumentOutOfRangeException if the joined string was empty.
Extract the logic into a GetUriPathBytes helper that strips exactly one
leading '/' when present and handles edge cases safely, replacing all 4
inline occurrences. Uses Segments-based logic (not Uri.AbsolutePath) to
preserve exact byte-compatibility with existing keychain entries.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
e174d4d to
12da03f
Compare
rolfbjarne
approved these changes
Jun 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The pattern
string.Join("", uri.Segments).Substring(1)appeared 4 times inKeychain.cs. It strips the leading/from the URI path, but would throwArgumentOutOfRangeExceptionfor URIs where the joined segments produce a string shorter than 2 characters (e.g. a host-only URI likehttp://host).Extracts the logic into a
GetUriPathByteshelper that safely handles edge cases, and replaces all inline occurrences.Testing
Build verified. The affected code paths require macOS keychain access, so no unit test was added.